package org.serverct.finalfight.stat;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.serverct.finalfight.FinalFight;
import org.serverct.finalfight.game.hadesgame.EventType;
import org.serverct.finalfight.stat.gui.InfoGuiListener;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

public class StatManager implements Listener {
    public static HashMap<UUID, IPlayer> stats = new HashMap<>();

    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
        StatManager.load(event.getPlayer());
    }

    @EventHandler
    public void onQuit(PlayerQuitEvent event) {
        StatManager.save(event.getPlayer());
    }

    public static void initialize() {
        for (Player player : Bukkit.getOnlinePlayers()) {
            load(player);
        }

        new BukkitRunnable() {
            @Override
            public void run() {
                for (Player player : Bukkit.getOnlinePlayers()) {
                    save(player);
                }
            }
        }.runTaskTimer(FinalFight.plugin, 0L, 1200L);

        Bukkit.getPluginManager().registerEvents(new StatManager(), FinalFight.plugin);
        Bukkit.getPluginManager().registerEvents(new InfoGuiListener(), FinalFight.plugin);
    }

    public static YamlConfiguration getYaml(Player player) {
        return YamlConfiguration.loadConfiguration(getFile(player));
    }

    public static File getFile(Player player) {
        File file = new File(FinalFight.plugin.getDataFolder() + "/playerstats/", player.getName() + ".yml");
        if (!file.exists()) {
            try {
                if (!file.createNewFile()) {
                    FinalFight.plugin.getLogger().warning("§c新建玩家数据时出现错误！");
                }
            } catch (IOException e) {
                FinalFight.plugin.getLogger().warning("§c数据读取错误，玩家: §e" + player.getName());
            }
        }
        return file;
    }

    public static void load(Player player) {
        YamlConfiguration yaml = getYaml(player);
        IPlayer stat;
        if (!yaml.contains("played")) {
            stat = new IPlayer(0, 0, 0, 0, new HashMap<>());
        } else {
            HashMap<EventType, Integer> experience = new HashMap<>();
            List<String> types = yaml.getStringList("experience.types");
            List<Integer> times = yaml.getIntegerList("experience.times");
            for (int index = 0; index < types.size(); index++) {
                experience.put(EventType.valueOf(types.get(index)), times.get(index));
            }
            stat = new IPlayer(yaml.getInt("played"),
                    yaml.getInt("won"),
                    yaml.getInt("kill"),
                    yaml.getInt("death"),
                    experience);
        }
        stats.put(player.getUniqueId(), stat);
    }

    public static void save(Player player) {
        YamlConfiguration yaml = getYaml(player);
        IPlayer stat = stats.get(player.getUniqueId());
        yaml.set("played", stat.played);
        yaml.set("won", stat.won);
        yaml.set("kill", stat.kill);
        yaml.set("death", stat.death);

        List<String> types = new ArrayList<>();
        List<Integer> times = new ArrayList<>();
        for (EventType type : stat.experience.keySet()) {
            types.add(type.name());
            times.add(stat.experience.get(type));
        }
        yaml.set("experience.types", types);
        yaml.set("experience.times", times);
        try {
            yaml.save(getFile(player));
        } catch (IOException e) {
            FinalFight.plugin.getLogger().warning("§c数据读取错误，玩家: §e" + player.getName());
        }
    }
}
